package manager

import (
	"gmap-backend/internal/config"
	"gmap-backend/internal/util/commonutil"
	"runtime"
	"strings"
	"syscall"
	"unsafe"
)

// GetClipTxt 获得剪切板的文本
func GetClipTxt(config *config.AppConfig) string {
	runtime.LockOSThread()
	defer runtime.UnlockOSThread()
	ptr, _, _ := config.Win32LibConfig.GetClipTxt.Call()
	defer config.Win32LibConfig.ReleaseStr.Call(ptr)
	return strings.TrimSpace(commonutil.GetSyscallUtf16Str(ptr))
}

// GetClipFiles 获得剪切板文件项
func GetClipFiles(config *config.AppConfig) []string {
	runtime.LockOSThread()
	defer runtime.UnlockOSThread()
	ptr, _, _ := config.Win32LibConfig.GetClipFiles.Call()
	defer config.Win32LibConfig.ReleaseStr.Call(ptr)
	str := commonutil.GetSyscallUtf16Str(ptr)
	if "" == str {
		return []string{}
	}
	return strings.Split(str, "|")
}

func SaveClipImgToFile(config *config.AppConfig, savePath string) bool {
	runtime.LockOSThread()
	defer runtime.UnlockOSThread()
	utf16Ptr, _ := syscall.UTF16PtrFromString(savePath)
	rs, _, _ := config.Win32LibConfig.SaveClipImgToFile.Call(uintptr(unsafe.Pointer(utf16Ptr)))
	if 0 == rs {
		return true
	}
	return false
}

func GetClipHasImgObj(config *config.AppConfig) bool {
	runtime.LockOSThread()
	defer runtime.UnlockOSThread()
	rs, _, _ := config.Win32LibConfig.ClipContainsImg.Call()
	println("rs is ", rs)
	if 1 == rs {
		return true
	}
	return false
}
